import { createStore } from "vuex";

const store = createStore({
  state() {
    return {
      cityName: localStorage.getItem("cityName") || "全国",
      cityId: localStorage.getItem("cityId") || "100001",
    };
  },
  getters: {},
  mutations: {
    changeName(state, payload) {
      state.cityName = payload.name;
      state.cityId = payload.cityId;
      localStorage.setItem("cityName", payload.name);
      localStorage.setItem("cityId", payload.cityId);

      if (localStorage.getItem("histroyCity")) {
        localStorage.setItem(
          "histroyCity",
          JSON.stringify(
            JSON.parse(localStorage.getItem("histroyCity")).concat(payload.name)
          )
        );
      } else {
        localStorage.setItem("histroyCity", JSON.stringify([payload.name]));
      }
    },
  },
  actions: {},
});

export default store;
